<?php
namespace Home\Controller;

use Think\Page;
use app\Home\Controller\Base;
class DiscoverController extends BaseController
{
    public function index()
    {
        $user_id=$this->user_info['id'];
        $agent1=M('agent')->where(['uid'=>$user_id,'status'=>['in',[1,2]]])->find();
//        dump($user_id);die;
        if($agent1){
            $status=1;
        }else{
            $status=0;
        }
        $this->assign('status',$status);
        $this->display();
    }

    public function sign(){
        if (IS_POST){
            $data = $this->user->find($this->user_info['id']);
            if(!$data){
                ajax_return(1,'该用户不存在');
            }
            $today=strtotime(date('Y-m-d'));
            if($data['sign_time']<$today){
                $config=M('reward_config')->field('sign1,sign2')->find();
//                $rand = rand($config['sign1'],$config['sign2']);
                $rand = $this->randfloat($config['sign1'],$config['sign2'],3);
                $z1=$data['z1']+$rand;
//                $this->user->save(['sign_time'=>time(),'z5'=>$z5],['id'=>$this->user_info['id']]);//更新签到时间
                M('user')->where(['id'=>$this->user_info['id']])->save(['sign_time'=>time(),'z1'=>$z1]);//更新签到时间
                $this->add_fmoney($this->user_info['id'],$this->user_info['id'],$rand,1,100,'签到');
                ajax_return(0,'签到成功，+'.$rand);
            }else{
                ajax_return(1,'今天已经签过到');
            }
        }
    }

    public function randfloat($min,$max,$decimal=''){
        $rand = $min + mt_rand()/mt_getrandmax()*($max-$min);
        if (empty($decimal)){
            return $rand;
        }else{
//        return floatval(number_format($rand,$decimal));
            return round($rand,$decimal);
        }

    }

    /**
     * @Notes:财商学院
     * @author: Qian
     * @Time: 2020/11/25 9:37
     */
    public function college(){

        $notice = M('news')->where(array('type'=>5,'language'=>2,'status'=>1))->order('id desc')->select();

        $this->assign('notice',$notice);
        $this->display();
    }

    /**
     * CBP 商家
     * User: ming
     * Date: 2020/12/3 17:36
     */
    public function shopping_stock(){
        $user_id = $this->user_info['id'];
        $user=M('user')->where(array('id'=>$user_id))->find();

        $count=M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])->count();
        $Page = new \Think\Page($count, 5);
        $Page->rollPage = 8; //控制最大页码显示数放在实例化page后面就成
        $show = $Page->show();
        $data = M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();
        $config=M('reward_config')->find();
        $this->assign('data', $data);
        $this->assign('page', $show);
        $this->assign('user',$user);
        $this->assign('config',$config);

        $this->display();
    }

    /**
     * CBP 商家申请
     * User: ming
     * Date: 2020/12/3 17:53
     */
    public function shopping_transfer(){
        //用户id
        $user_id = $this->user_info['id'];
//       $month_time= date("Y-m-d H:i:s", strtotime("+1 months", $lockData['addtime']));//下个月时间
        //获取账户信息
        $userData = M('user')->where(['id' => $user_id])->find();
        $this->assign('userData', $userData);
        //获取手续费参数
        $configData = M('reward_config')->find();
        $this->assign('configData', $configData);

        if (IS_POST) {
//            if ($userData['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '请先到个人中心完成实名认证'));
////                ajax_return(1, '请先到个人中心完成实名认证');
//            }

            //接收参数
            $data = I('post.');
            if ($userData['password2'] != MD5($data['password2'])) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '请输入正确的支付密码'));
            }
//            if($configData['canshu69'] == 0 )
//            {
//                $this->ajaxReturn(array('code'=>'-1','message'=>$configData['canshu70']));
//            }
//            if ($data['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-2', 'message' => '请先完成实名认证'));
//            }

            if ($data['amount'] > $userData['z8']) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '账户余额不足'));
            }
            M()->startTrans();
            $arr['z17']=$userData['17']+$data['amount'];
            $arr['z8']=$userData['z8']-$data['amount'];
            $arr['is_shopping']=2;
            $res1= M('user')->where(['id'=>$user_id])->save($arr);
            $res2 = $this->add_fmoney_log($user_id, $user_id, $data['amount'], 17, '商家申请',  "13", $data['amount'],time());
            if ($res1&&$res2) {
                M()->commit();
                $this->ajaxReturn(array('code' => '1', 'message' => '恭喜您申请成功'));
            } else {
                M()->rollback();
                $this->ajaxReturn(array('code' => '-1', 'message' => '申请失败'));
            }
        } else {
            //获取存币生息提现记录
//            $where['user_id'] = $user_id;
//            $where['from_id'] = $lid;
//            $where['type'] = ['in', [7, 11]];//存币生息ASSET收益EASY收益
////            $where['reward_type'] = 712;
//            $where['reward_type'] = ['in', [712, 1112]];
//            $count = M('fmoney')->where($where)->count();
//            $Page = new \Think\Page($count, 4);
////            $Page->setConfig(prev, '上一页');
////            $Page->setConfig(next, '下一页');
//            $show = $Page->show();
//            $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//            $this->assign('chargeData', $data);
//            $this->assign('page', $show);
            $this->display();
        }
    }

    /**
     * 商家申请记录
     * User: ming
     * Date: 2020/12/5 15:48
     */
    public function shopping_list()
    {
        //用户id
        $user_id = $this->user_info['id'];

        //获取锁仓记录
//
//        $count = M('locked')->where($where)->count();
//        $Page = new \Think\Page($count, 8);
//        $show = $Page->show();
//        $data = M('locked')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//        $this->assign('lockData', $data);
//        $this->assign('page', $show);
//        $config = M('reward_config')->where('id=1')->field('finance_toggle')->find();
//        $this->assign('config',$config);
//
//        $this->display();

        $where['userid'] = $user_id;
        $where['reward_type'] = array('in',[13]);
        $where['type'] = 17;

        $count=M('fmoney')->where($where)->count();
        $page=new Page($count,20);
        $show=$page->show();
        $record=M('fmoney')->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($record as $k=>$v){
        $user=M('user')->where(array('id'=>$v['user_id']))->find();
            $record[$k]['username']=$user['username'];
        }
        $this->assign('data',$record);
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->display();
    }
    /**
     * 股本记录
     * User: ming
     * Date: 2020/11/30 11:52
     */
    public function stock(){
        $user_id = $this->user_info['id'];
        $user=M('user')->where(array('id'=>$user_id))->find();

        $count=M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])->count();
        $Page = new \Think\Page($count, 5);
        $Page->rollPage = 8; //控制最大页码显示数放在实例化page后面就成
        $show = $Page->show();
        $data = M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();
        $config=M('reward_config')->find();
        $this->assign('data', $data);
        $this->assign('page', $show);
        $this->assign('user',$user);
        $this->assign('config',$config);

        $this->display();
    }

    /**
     * 股本详情
     * User: ming
     * Date: 2020/12/1 17:49
     */
    public function stock_detail(){
        $user_id = $this->user_info['id'];
        $user=M('user')->where(array('id'=>$user_id))->find();

        $count=M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])->count();
        $Page = new \Think\Page($count, 5);
        $Page->rollPage = 8; //控制最大页码显示数放在实例化page后面就成
        $show = $Page->show();
        $data = M('fmoney')->where(['user_id'=>$user_id,'type'=>1,'reward_type'=>19])
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();
        $config=M('reward_config')->find();
        $user['gh']=($user['z16']-$user['z17'])/$config['a42']*$config['a40'];
        $this->assign('data', $data);
        $this->assign('page', $show);
        $this->assign('user',$user);
        $this->assign('config',$config);

        $this->display();
    }

    /**
     * 股本记录
     * User: ming
     * Date: 2020/12/5 16:29
     */
    public function stock_list(){
        $user_id = $this->user_info['id'];
        $user=M('user')->where(array('id'=>$user_id))->find();

//        $count=M('fmoney')->where(['user_id'=>$user_id,'type'=>17,'reward_type'=>12])->count();
        $count=M('fmoney')->where(['user_id'=>$user_id,'type'=>17,'reward_type'=>array('in',[12,16])])->count();
        $Page = new \Think\Page($count, 5);
        $Page->rollPage = 8; //控制最大页码显示数放在实例化page后面就成
        $show = $Page->show();
        $data = M('fmoney')->where(['user_id'=>$user_id,'type'=>17,'reward_type'=>array('in',[12,16])])
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('create_time desc')
            ->select();
        $config=M('reward_config')->find();

        $this->assign('data', $data);
        $this->assign('page', $show);
        $this->assign('user',$user);
        $this->assign('config',$config);

        $this->display();
    }
    /**
     * 股本划转
     * User: ming
     * Date: 2020/12/2 11:52
     */
    public function stock_transfer(){
//        $lid = I('lid') ? I('lid') : 0;
        //用户id
        $user_id = $this->user_info['id'];

//       $month_time= date("Y-m-d H:i:s", strtotime("+1 months", $lockData['addtime']));//下个月时间
//        dump($month_time); die;
        //获取账户信息
        $userData = M('user')->where(['id' => $user_id])->find();
        $this->assign('userData', $userData);
        //获取手续费参数
        $configData = M('reward_config')->find();
        $this->assign('configData', $configData);
        if (IS_POST) {
//            if ($userData['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-1', 'message' => '请先到个人中心完成实名认证'));
////                ajax_return(1, '请先到个人中心完成实名认证');
//            }

            //接收参数
            $data = I('post.');
            if ($userData['password2'] != MD5($data['password2'])) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '请输入正确的支付密码'));
            }
//            if($configData['canshu69'] == 0 )
//            {
//                $this->ajaxReturn(array('code'=>'-1','message'=>$configData['canshu70']));
//            }
//            if ($data['is_real'] != 2) {
//                $this->ajaxReturn(array('code' => '-2', 'message' => '请先完成实名认证'));
//            }

            if($data['amount']<5000){
                $this->ajaxReturn(array('code' => '-1', 'message' => '不能低于5000'));
            }
            if ($data['amount'] > $userData['z8']) {
                $this->ajaxReturn(array('code' => '-1', 'message' => '账户余额不足'));
            }
            M()->startTrans();
            $arr['z17']=$userData['z17']+$data['amount'];
            $arr['z16']=$userData['z16']+$data['amount'];
            $arr['z8']=$userData['z8']-$data['amount'];
            $res1= M('user')->where(['id'=>$user_id])->save($arr);
            $res2 = $this->add_fmoney_log($user_id, $user_id, $data['amount'], 17, '划转股本',  "12", $data['amount'],time());
            if ($res1&&$res2) {
                M()->commit();
                $this->ajaxReturn(array('code' => '1', 'message' => '划转成功'));
            } else {
                M()->rollback();
                $this->ajaxReturn(array('code' => '-1', 'message' => '划转失败'));
            }
        } else {
            //获取存币生息提现记录
//            $where['user_id'] = $user_id;
//            $where['from_id'] = $lid;
//            $where['type'] = ['in', [7, 11]];//存币生息ASSET收益EASY收益
////            $where['reward_type'] = 712;
//            $where['reward_type'] = ['in', [712, 1112]];
//            $count = M('fmoney')->where($where)->count();
//            $Page = new \Think\Page($count, 4);
////            $Page->setConfig(prev, '上一页');
////            $Page->setConfig(next, '下一页');
//            $show = $Page->show();
//            $data = M('fmoney')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//            $this->assign('chargeData', $data);
//            $this->assign('page', $show);
            $this->display();
        }
    }

    /**
     * 查询密码确认
     * User: ming
     * Date: 2020/12/3 17:19
     */
    public function stock_password(){
        if (IS_POST) {
            $user_id = $this->user_info['id'];

            //获取账户信息
            $userData = M('user')->where(['id' => $user_id])->find();
            //接收参数
            $data = I('post.');
//            $this->ajaxReturn(array('code' => '-1', 'msg' =>$userData['password2']));
            if ($userData['password2'] != MD5($data['password2'])) {
                $this->ajaxReturn(array('code' => '-1', 'msg' => '请输入正确的支付密码'));
            }
            $this->ajaxReturn(array('code' => '1', 'msg' => '密码正确!'));
        }
        $this->ajaxReturn(array('code' => '-1', 'msg' => '数据异常'));
    }
}